import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPainter, QPixmap, QColor, QBrush
from PyQt5.QtCore import Qt, QPoint
from PyQt5 import QtWidgets, QtGui
import cv2 as cv


class Winform(QWidget):
    def __init__(self, parent=None):
        super(Winform, self).__init__(parent)
        # 设置标题
        self.setWindowTitle("绘图例子")
        # 实例化QPixmap类
        self.pix = QPixmap()
        self.fistpoint = QPoint()
        # 起点，终点
        self.lastPoint = QPoint()
        self.endPoint = QPoint()
        # 初始化
        self.initUi()

    def initUi(self):
        # self.resize(600, 500)  # 窗口大小设置为600*500，这样可以鼠标拖动缩放
        self.setFixedSize(356, 256)  # 固定窗口大小，不可缩放

        # 画布大小为400*400，背景为白色
        self.pix = QPixmap(256, 256)
        self.pix.fill(Qt.black)

        # 偏移量，保证鼠标的位置和画的线点是重合的
        self.offset = QPoint(self.width() - self.pix.width(), self.height() - self.pix.height())

        btn_clear = QPushButton(self)
        btn_clear.setText("清空")
        btn_clear.resize(80, 30)
        btn_clear.move(10, 30)
        btn_clear.clicked.connect(self.clear)

        btn_save = QPushButton(self)
        btn_save.setText("保存")
        btn_save.resize(80, 30)
        btn_save.move(10, 80)
        btn_save.clicked.connect(self.save)

        btn_fill = QPushButton(self)
        btn_fill.setText("填充")
        btn_fill.resize(80, 30)
        btn_fill.move(10, 130)
        btn_fill.clicked.connect(self.fill)

    def clear(self):
        self.pix.fill(Qt.black)
        self.update()

    def save(self):
        self.pix.save("lib/temp/draw.jpg")

    def fill(self):
        img = cv.imread("lib/temp/draw.jpg")
        img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
        ret, binary = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY)
        contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

        cv.fillPoly(img, [contours[1]], (0, 0, 125))
        cv.imwrite('lib/temp/test/temp.jpg', img)

    def paintEvent(self, event):
        pp = QPainter(self.pix)
        pp.setPen(QColor(255, 255, 255))
        # 根据鼠标指针前后两个位置绘制直线
        pp.drawLine(self.lastPoint, self.endPoint)
        # 让前一个坐标值等于后一个坐标值，
        # 这样就能实现画出连续的线
        self.lastPoint = self.endPoint
        painter = QPainter(self)
        # 绘制画布到窗口指定位置处
        painter.drawPixmap(100, 0, self.pix)

    def mousePressEvent(self, event):
        # 鼠标左键按下
        if event.button() == Qt.LeftButton:
            self.lastPoint = event.pos() - self.offset
            # 上面这里减去一个偏移量，否则鼠标点的位置和线的位置不对齐
            self.fistpoint = self.lastPoint
            self.endPoint = self.lastPoint
            print(self.endPoint)

    def mouseMoveEvent(self, event):
        # 鼠标左键按下的同时移动鼠标
        if event.buttons() and Qt.LeftButton:
            self.endPoint = event.pos() - self.offset
            # 进行重新绘制
            self.update()

    def mouseReleaseEvent(self, event):
        # 鼠标左键释放
        if event.button() == Qt.LeftButton:
            self.endPoint = self.fistpoint
            # 进行重新绘制
            self.update()
